package com.itcast.service.impl;


import com.github.pagehelper.PageHelper;
import com.itcast.dao.BookDao;
import com.itcast.pojo.po.User;
import com.itcast.pojo.vo.*;
import com.itcast.service.Sercicess;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ServicesImpl implements Sercicess {

    @Autowired
    BookDao bookDao;
    @Autowired
    Result result;
    public boolean ss = false;

    @Override
    public Result serviceUser(QueryPageBean queryPageBean) {

        if (queryPageBean.getCurrentPage() == 0 || queryPageBean.getPageSize() == 0) {
            result.setFlag(ss);
            result.setMessage("参数异常");
        }
        System.out.println(queryPageBean.getCurrentPage());
        System.out.println(queryPageBean.getPageSize());
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<User> users = bookDao.selectUser();
        int size = users.size();
        PageResult pageResult = new PageResult();
        pageResult.setTotal((long) size);
        pageResult.setRows(users);
        result.setFlag(ss = true);
        result.setMessage("添加成功");
        result.setResult(users);
        return result;

    }

    @Override
    public Result insetuser(AddUser addUser) {

        Integer integer = bookDao.insertUser(addUser);
        if (integer == 0) {
            result.setFlag(ss);
            result.setMessage("失败");
            return result;
        } else {
            List<String> roleIds = addUser.getRoleIds();
            for (String roleId : roleIds) {
                int i = Integer.parseInt(roleId);
                Integer insertrole = bookDao.insertrole(addUser.getId(), i);
                if (insertrole == 0) {
                    result.setFlag(ss);
                    result.setMessage("添加失败");
                    return result;
                } else {
                    result.setFlag(ss = true);
                    result.setMessage("添加成功");

                }
            }

        }
        return result;

    }


    @Override
    public Result updata(UpdateUser updateUser) {
        Integer insertrole = 0;
        if (updateUser == null) {
            result.setFlag(ss);
            result.setMessage("参数为空");
            return result;
        } else {
            User selectname = bookDao.selectname(updateUser);
            if (selectname == null) {
                result.setFlag(ss);
                result.setMessage("用户已不存在");
                return result;
            } else {
                Integer delete = bookDao.tuserrole(updateUser.getId());
                if (delete == 0) {
                    result.setFlag(ss);
                    result.setMessage("删除角色失败");
                    return result;
                } else {
                    List<String> roleIds = updateUser.getRoleIds();
                    for (String roleId : roleIds) {
                        int i = Integer.parseInt(roleId);
                        System.out.println(i);
                        insertrole = bookDao.insertrole(updateUser.getId(), i);
                    }
                        if (insertrole == 0) {
                            result.setFlag(ss);
                            result.setMessage("添加失败");
                            return result;
                        } else {
                            Integer updata = bookDao.updata(updateUser);
                            if (updata == 0) {
                                result.setFlag(ss);
                                result.setMessage("跟改角色失败");
                            } else {
                                result.setFlag(ss = true);
                                result.setMessage("跟改成功");
                            }

                        }
                }
            }
        }
        return result;
    }



    @Override
    public Result dalete(Integer id) {
        Integer delete = bookDao.tuserrole(id);
        if (delete == 0) {
            result.setFlag(ss);
            result.setMessage("删除角色失败");
        } else {
            Integer integer = bookDao.deleteUser(id);
            if (delete == 0) {
                result.setFlag(ss);
                result.setMessage("删除对象失败");
            } else {
                result.setFlag(ss = true);
                result.setMessage("删除成功");
            }

        }

        return result;
    }
}




